package com.erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.Util.WebUtil;
import com.erp.comm.DataAndView;

import com.erp.comm.ResultCode;
import com.erp.comm.ReturnGood;
import com.erp.pojo.*;
import com.erp.service.GoodsService;
import com.erp.service.InportService;
import com.erp.service.OutportService;
import com.erp.service.ProviderService;
import com.erp.vo.OutportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("outport")
public class OutportController {

    @Autowired
    private OutportService outportService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ProviderService providerService;
    @Autowired
    private InportService inportService;

    @RequestMapping("loadAllOutport")
    public DataAndView loadAllOutport(OutportVo outportVo){

        IPage<Outport> page = new Page<Outport>(outportVo.getPage(),outportVo.getLimit());


        QueryWrapper<Outport> qw = new QueryWrapper<>();
        //如果条件不为空 就添加
        if (outportVo.getProviderid() != null && outportVo.getProviderid()!=0){
            qw.like("providerid",outportVo.getProviderid());
        }
        if (outportVo.getProviderid() != null && outportVo.getGoodsid() !=0){
            qw.like("goodsid",outportVo.getGoodsid());
        }

        //把string转化为date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        Date endTime = null;
        if (outportVo.getStartTime()!=null && outportVo.getStartTime() != ""){
            try {
                startTime = sdf.parse(outportVo.getStartTime());
            }catch (Exception e){
            }
            qw.gt("outputtime",new Timestamp(startTime.getTime()));
        }
        if (outportVo.getEndTime()!=null && outportVo.getEndTime() != ""){
            try {
                endTime = sdf.parse(outportVo.getEndTime());
            }catch (Exception e){
            }
            qw.lt("outputtime",new Timestamp(endTime.getTime()));

        }

        if (outportVo.getOperateperson() != null){

            qw.like("operateperson",outportVo.getOperateperson());
        }
        if (outportVo.getRemark() != null){
            qw.like("remark",outportVo.getRemark());
        }
        outportService.page(page,qw);
        List<Outport> list = page.getRecords();
        List<Outport> list2 = new ArrayList<>();
        for (Outport outport:list){
            //获取当前商品的供应商id
            Integer provideId = outport.getProviderid();
            //通过供应商查id对应的供应商
            Provider provider = providerService.getById(provideId);
            //设置当前的商品的供货名称
            outport.setProvidername(provider.getProvidername());

            Integer goodsId = outport.getGoodsid();
            Goods good = goodsService.getById(goodsId);
            //设置商品名称
            outport.setGoodsname(good.getGoodsname());
            //设置商品规格
            outport.setSize(good.getSize());

            list2.add(outport);
        }
        return  new DataAndView(list2);
    }
    //添加
    @RequestMapping("addOutport")
    public ResultCode addOutport(ReturnGood returnGood){
        System.out.println(returnGood);
        //获取当前进货数据
        Inport inport= inportService.getById(returnGood.getId());
        //设置退货信息
         Outport outport= new Outport();
         Goods good=goodsService.getById(inport.getGoodsid());
         //设置商品名称
         outport.setGoodsname(good.getGoodsname());
         outport.setProvidername(providerService.getById(inport.getProviderid()).getProvidername());
         outport.setSize(good.getSize());
         outport.setGoodsid(inport.getGoodsid());
         outport.setNumber(returnGood.getNumber());
        HttpSession session =WebUtil.getSession();
        User user =(User) session.getAttribute("user");
         outport.setOperateperson(user.getName());
         outport.setOutputtime(new Date());
         outport.setOutportprice(inport.getInportprice());
         outport.setRemark(returnGood.getRemark());
         outport.setPaytype(inport.getPaytype());
         outport.setProviderid(inport.getProviderid());
         //退货 数量减少

       Long num1 = returnGood.getCurrentnumber();
       long num2 = num1;
       int num3 = (int)num2;
       inport.setNumber(num3-returnGood.getNumber());
       if (inport.getNumber()==0){
           inportService.removeById(inport);
       }else {
           inportService.updateById(inport);
       }
       boolean boof  =outportService.save(outport);
        if (boof){
            return ResultCode.SUCCESS_ADD;
        } else {
            return ResultCode.FAIL_ADD;
        }









    }
}
